package com.qc.AirQuality.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qc.AirQuality.util.BaseDao;

public class AirQualityIndexDao {
	
	BaseDao dao = new BaseDao();

	public List<Map<String, Object>> findAll1(Integer districtId,Integer pageIndex,Integer pageSize) {
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		StringBuffer sql = new StringBuffer("select a.id,d.`name`,a.monitor_time,a.pm10,a.pm25,a.monitoring_station from air_quality_index as a INNER JOIN district as d on d.id = a.district_id where 1 = 1"); 
		if(districtId != null){
			sql.append(" and a.district_id = ");
			sql.append(districtId);
		}
		
		sql.append(" limit ");
		sql.append((pageIndex - 1) * pageSize);
		sql.append(",");
		sql.append(pageSize);
		
		Connection connection = dao.getConnection();
		ResultSet rs = null;
		PreparedStatement ps = null;
		
		try {
			ps = connection.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			
			while(rs.next()){
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("id",rs.getInt("id"));
				map.put("name",rs.getString("name"));
				map.put("monitorTime",rs.getString("monitor_time"));
				map.put("pm10",rs.getString("pm10"));
				map.put("pm25",rs.getString("pm25"));
				map.put("monitoringStation",rs.getString("monitoring_station"));
				
				list.add(map);
				
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			dao.close(rs, ps, connection);
		}
		
		
		
		return list;
	}
}







